package com.jia.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.config.CustomRabbitMq;
import com.jia.dto.admin.UserDto;
import com.jia.entity.DepositUser;
import com.jia.entity.User;
import com.jia.mapper.DepositUserMapper;
import com.jia.service.UserService;
import com.jia.mapper.UserMapper;
import com.jia.vo.admin.ComboVo;
import com.jia.vo.admin.DepositUserInfo;
import com.jia.vo.admin.DepositUserVo;
import com.jia.vo.admin.MainInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 贾天旺
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2024-04-26 14:49:12
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepositUserMapper depositUserMapper;

//    @Autowired
//    private CustomRabbitMq customRabbitMq;

    /**
     * 用户管理列表
     * @param page
     * @param limit
     * @param userDto
     * @return
     */
    @Override
    public Page<User> userList(Integer page, Integer limit, UserDto userDto) {
        if (ObjectUtil.isNotEmpty(userDto.getRealname()) && ObjectUtil.isNotEmpty(userDto.getPhone()) && ObjectUtil.isNotEmpty(userDto.getCreateTimeBegin()) && ObjectUtil.isNotEmpty(userDto.getCreateTimeEnd())) {
            return this.page(new Page<>(page, limit), new QueryWrapper<User>()
                    .like("realname", userDto.getRealname())
                    .like("phone", userDto.getPhone())
                    .eq("state", userDto.getState())
                    .between("createtime", userDto.getCreateTimeBegin(), userDto.getCreateTimeEnd()).orderByDesc("createtime"));
        } else if (ObjectUtil.isNotEmpty(userDto.getRealname()) && ObjectUtil.isNotEmpty(userDto.getPhone())) {
            return this.page(new Page<>(page, limit), new QueryWrapper<User>()
                    .like("realname", userDto.getRealname())
                    .like("phone", userDto.getPhone())
                    .eq("state", userDto.getState())
                    .orderByDesc("createtime"));
        } else if (ObjectUtil.isNotEmpty(userDto.getRealname()) || ObjectUtil.isNotEmpty(userDto.getPhone())) {
            return this.page(new Page<>(page, limit), new QueryWrapper<User>()
                    .like("realname", userDto.getRealname())
                    .like("phone", userDto.getPhone())
                    .eq("state", userDto.getState())
                    .orderByDesc("createtime"));
        } else if (ObjectUtil.isNotEmpty(userDto.getCreateTimeBegin()) && ObjectUtil.isNotEmpty(userDto.getCreateTimeEnd())) {
            return this.page(new Page<>(page, limit), new QueryWrapper<User>()
                    .between("createtime", userDto.getCreateTimeBegin(), userDto.getCreateTimeEnd())
                    .eq("state", userDto.getState())
                    .orderByDesc("createtime"));
        }
        return this.page(new Page<>(page, limit), new QueryWrapper<User>()
                .eq("state", userDto.getState())
                .orderByDesc("createtime"));
    }

    @Override
    public void updateState(int id) {
        User user = this.getById(id);
        if (ObjectUtil.isNotEmpty(user)) {
            user.setState(user.getState() == 1 ? 0 : 1);
            this.updateById(user);
        }
    }

    /**
     * 用户记录
     * @param page
     * @param limit
     * @param userDto
     * @return
     */
    @Override
    public Page<DepositUserVo> depositUser(Integer page, Integer limit, UserDto userDto) {
        return userMapper.depositUser(new Page<DepositUserVo>(page,limit),userDto);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteUserDeposit(int id) {
        userMapper.deleteUserDeposit(id);
    }

    /**
     * 退还
     * @param id
     */
    @Override
    public void getReturnDeposit(int id) {
        User one = this.userMapper.selectOne(new QueryWrapper<User>().eq("id", id));
        if(ObjectUtil.isNotEmpty(one)){
            //进行插入中间
            DepositUser depositUser=new DepositUser();
            depositUser.setUid(one.getId());
            depositUser.setAmount(BigDecimal.valueOf(one.getDeposit()));
            depositUser.setState(0);
            depositUser.setCreatetime(new Date());
            depositUser.setUpdatetime(new Date());
            int insert = this.depositUserMapper.insert(depositUser);
            if(insert>=1){
                //进行押金修改
                one.setDeposit(0);
                this.updateById(one);
            }
        }
    }

    /**
     * 柱状
     * @param charItem
     * @return
     */
    @Override
    public DepositUserInfo depositUserInfo(String charItem) {
        List<DepositUserInfo> depositUserInfo=null;
        switch (charItem){
            case "year":
                depositUserInfo=this.depositUserMapper.DEPOSIT_USER_INFOSYear();
                break;
            case "month":
                depositUserInfo=this.depositUserMapper.DEPOSIT_USER_INFOSMonth();
                break;
            default:
                depositUserInfo = this.depositUserMapper.depositUserInfo();
                break;
        }
        List<String> orderDate = depositUserInfo.stream().map(DepositUserInfo::getOrderDate).collect(Collectors.toList());
        List<Integer> orderCount = depositUserInfo.stream().map(DepositUserInfo::getOrderCount).collect(Collectors.toList());

        //为depositUserInfo1赋值
        DepositUserInfo depositUserInfo1=new DepositUserInfo();
        depositUserInfo1.setOrderdates(orderDate);
        depositUserInfo1.setOrdercounts(orderCount);

        return depositUserInfo1;
    }

    /**
     * 线图
     * @param item
     * @return
     */
    @Override
    public ComboVo comboInfo(String item) {
        List<ComboVo> comboVos=null;
      switch (item){
          case "day":
              comboVos=this.depositUserMapper.comboInfoDay();
              break;
          case "year":
              comboVos=this.depositUserMapper.comboInfoYear();
              break;
          case "seven":
              comboVos=this.depositUserMapper.comboInfoDAY7();
              break;
          default:
              comboVos=this.depositUserMapper.comboInfo();
              break;
      }
        List<String> comboDate=comboVos.stream().map(ComboVo::getMonth).collect(Collectors.toList());
        List<BigDecimal> comboCount=comboVos.stream().map(ComboVo::getAmount).collect(Collectors.toList());
        comboCount.forEach(System.err::println);
        ComboVo comboVo=new ComboVo();
        comboVo.setBigDecimals(comboCount);
        comboVo.setMonths(comboDate);
        return comboVo;
    }

    @Override
    public List<User> depositList() {
//        List<User> list = customRabbitMq.getUsers();
        User user=new User();
        user.setDeposit(1000);
        List<User> list=new ArrayList<>();
        list.add(user);
        System.out.println("业务层中"+list.size());
        list.forEach(System.out::println);
        return list;
    }


}


